package  wibrst.visual {
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * ... wibrst.visual.VMcPlayable
	 * @author wibrst@qq.com
	 */
	public class VMcPlayable extends MovieClip {
		
		static public const PLAY_END:String = "PLAY_END";
		
		public function VMcPlayable() {
			_nHomeFrame = 1;
		}
		
		/// dump
		public function setHomeFrame( nFrame:Number ):void {
			_nHomeFrame = nFrame;
		}
		
		/// control
		public function stopAtHome():void {
			this.removeEventListener( Event.ENTER_FRAME, engineHandler );
			this.gotoAndStop( _nHomeFrame );
		}
		
		public function playFromHome( bAddDet:Boolean = true, bStopEnd:Boolean = true ):void {
			//trace( this, "playFromHome _nHomeFrame:", _nHomeFrame );
			if ( bAddDet ) this.addPlayDetect( bStopEnd );
			this.gotoAndPlay( _nHomeFrame );
		}
		
		public function stopAtEnd():void {
			this.removeEventListener( Event.ENTER_FRAME, engineHandler );
			this.gotoAndStop( this.totalFrames );
		}
		
		/// add detect
		public function addPlayDetect( bStopEnd:Boolean = true ):void {
			this._bStopEnd = bStopEnd;
			this.removeEventListener( Event.ENTER_FRAME, engineHandler );
			super.addEventListener( Event.ENTER_FRAME, engineHandler );
		}
		
		private function engineHandler(e:Event):void {
			if ( this.currentFrame == this.totalFrames ) {
				if ( _bStopEnd ) this.stop();
				super.dispatchEvent( new Event( PLAY_END ) );
				this.removeEventListener( Event.ENTER_FRAME, engineHandler );
			}
		}
		
		/// dump
		private var _nHomeFrame:Number;
		
		/// control
		private var _bStopEnd:Boolean;
	}

}